/*
 *  DeleteUserAction.java
 *
 *  Course Project for User Interface Programming I, 2011
 *  Group 19
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 */
package justdoit.actions;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import justdoit.controller.TaskManager;
import justdoit.controller.LanguageManager;
import justdoit.controller.IconManager;

/**
 * This is for performing action of deleting particular user
 * when user clicks "Delete User" item
 * 
 * @author Can
 */
public class DeleteUserAction extends AbstractAction {

    public DeleteUserAction() {
        super(LanguageManager.getString("menu.user.deleteUser"), IconManager.getIcon("deleteUser.png"));
        putValue(MNEMONIC_KEY, LanguageManager.getMnemonic("mnemonic.menu.user.delete"));
        putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("alt D"));
    }

    public DeleteUserAction(String text) {
        super(text);
    }

    public DeleteUserAction(String text, ImageIcon icon) {
        super(text, icon);
    }

    public DeleteUserAction(String text, ImageIcon icon,
            String desc, Integer mnemonic) {
        super(text, icon);
        putValue(SHORT_DESCRIPTION, desc);
        putValue(MNEMONIC_KEY, mnemonic);
    }

    public void actionPerformed(ActionEvent e) {
        UIManager.put("OptionPane.okButtonText", LanguageManager.getString("button.ok"));
        UIManager.put("OptionPane.cancelButtonText", LanguageManager.getString("button.cancel"));
        while (true) {
            String password = JOptionPane.showInputDialog(null,
                    LanguageManager.getString("confirmDialog.deleteUserQuesion"),
                    LanguageManager.getString("confirmDialog.deleteUserTitle"),
                    JOptionPane.WARNING_MESSAGE | JOptionPane.OK_CANCEL_OPTION);
            if (password == null) {
                break;
            }
            if (TaskManager.getCurrentUser().matchPassword(password.toCharArray())) {
                if (TaskManager.deleteUser() == 0) {
                    System.exit(0);
                } else {
                    System.out.println("delete user not successful!");
                }
            }
        }
    }
}
